package cn.tq.pethome.base.service.impl;

import cn.tq.pethome.base.mapper.IBaseMapper;
import cn.tq.pethome.base.query.BaseQuery;
import cn.tq.pethome.base.service.IBaseService;
import cn.tq.pethome.bean.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private IBaseMapper<T> baseMapper;

    @Override
    public ResultBean<T> queryAll(BaseQuery query) {
        ResultBean<T> resultBean = new ResultBean<>();
        resultBean.setTotals(baseMapper.findCounts(query));
        resultBean.setPages(baseMapper.queryAll(query));
        return resultBean;
    }

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    @Transactional
    @Override
    public void insert(T t) {
        baseMapper.insert(t);
    }

    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Transactional
    @Override
    public void batchDelete(List<Long> ids) {
        baseMapper.batchDelete(ids);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }
}
